package com.sudoku.board;

import java.util.ArrayList;

import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.sudoku.observer.Subject;
import com.sudoku.utility.Constants;

public class Board implements Constants {

	/*
	 * data has boxes in order as shown below 0 3 6 1 4 7 2 5 8
	 */
	protected ArrayList<Box> data;

	public Board(Subject subject) {
		data = new ArrayList<Box>();
		for (int i = 0; i < 3; i++) {
			for (int j = 2; j >= 0; j--) {
				data.add(new Box(new Vector2(i, j), false, subject));
			}
		}
	}

	public void drawBoxes(SpriteBatch sb, BitmapFont font, BitmapFont pencilFont, ShapeRenderer sr) {
		for (Box b : data) {
			b.drawTiles(sb, font, pencilFont, sr);
		}
	}

	public void clear() {
		data.clear();
	}

	public ArrayList<Box> getData() {
		return data;
	}

	public void setData(ArrayList<Box> data) {
		this.data = data;
	}

	public ArrayList<Tile> getTilesInRow(int num) {
		ArrayList<Tile> tmp = new ArrayList<Tile>();
		tmp.addAll(data.get(num / 3).getTilesInBoxRow(num % 3));
		tmp.addAll(data.get(num / 3 + 3).getTilesInBoxRow(num % 3));
		tmp.addAll(data.get(num / 3 + 6).getTilesInBoxRow(num % 3));
		return tmp;
	}

	public ArrayList<Tile> getTilesInCol(int num) {
		ArrayList<Tile> tmp = new ArrayList<Tile>();
		tmp.addAll(data.get((num / 3) * 3).getTilesInBoxCol(num % 3));
		tmp.addAll(data.get((num / 3) * 3 + 1).getTilesInBoxCol(num % 3));
		tmp.addAll(data.get((num / 3) * 3 + 2).getTilesInBoxCol(num % 3));
		return tmp;
	}

	public ArrayList<Integer> getBoardRow(int num) {
		ArrayList<Integer> tmp = new ArrayList<Integer>();
		tmp.addAll(data.get(num / 3).getBoxRow(num % 3));
		tmp.addAll(data.get(num / 3 + 3).getBoxRow(num % 3));
		tmp.addAll(data.get(num / 3 + 6).getBoxRow(num % 3));
		return tmp;
	}

	public ArrayList<Integer> getBoardCol(int num) {
		ArrayList<Integer> tmp = new ArrayList<Integer>();
		tmp.addAll(data.get((num / 3) * 3).getBoxCol(num % 3));
		tmp.addAll(data.get((num / 3) * 3 + 1).getBoxCol(num % 3));
		tmp.addAll(data.get((num / 3) * 3 + 2).getBoxCol(num % 3));
		return tmp;
	}

	public static void PrintBoard(Board board) {
		for (int i = 0; i < 9; i++) {
			ArrayList<Tile> row = board.getTilesInRow(i);
			for (int j = 0; j < 9; j++) {
				if (row.get(j).isBlank()) {
					System.out.print(". ");
				} else {
					System.out.print(row.get(j).getData() + " ");
				}
			}
			System.out.println("");
		}
	}

	public void copyData(Board otherBoard) {
		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				this.data.get(i).getData().get(j).setData(otherBoard.getData().get(i).getData().get(j).getData());
			}
		}
	}

	public Tile getDataFromIndex(int index) {
		Tile data = this.getTilesInRow(8 - (index / 9)).get(index % 9);
		return data;
	}

	public void setBlankToIndex(int index) {
		this.getTilesInRow(8 - (index / 9)).get(index % 9).setBlank();

	}

	public static int CharToInt(char c) {
		return Integer.valueOf(String.valueOf(c));
	}

	public static char IntToChar(Integer i) {
		return Character.forDigit(i, 10);
	}

}
